package com.appspot.rsp.rpc;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;

public class RspXmppReceiverServlet extends HttpServlet {
	private static final XMPPService xmppService = XMPPServiceFactory.getXMPPService();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// The Java Servlet API does not provide a way to parse MIME 
		// multipart POST data directly.
		// The XMPP API includes a helper method for parsing this data from 
		// the HttpServletRequest object.
        Message message = xmppService.parseMessage(req);

		// the address of the sender of the message
        JID fromJid = message.getFromJid();
		// the address of the recipient as described by the sender.
        JID[] toJids = message.getRecipientJids();
		// the body of the message
        String body = message.getBody();
		// the full XMPP message in its original XML form
		String stanza = message.getStanza();
		resp.getWriter().println(fromJid);
		resp.getWriter().println(toJids[0]);
		resp.getWriter().println(body);
		resp.getWriter().println(stanza);
	}
}
